# -*- coding:utf-8 -*-

import json
import requests
import time
from celery_worker import app
from parser.xmlparser import XmlParser

__author__ = 'ghostviper'


xml_parser = XmlParser()


@app.task
def parser(html, extract_field_rules):
    """
    解析字段
    :param html:
    :param extract_field_rules:
    :return:
    """
    result = list()
    for field_name, rule in extract_field_rules['extract_rules'].items():
        result += xml_parser.field_extract(html, field_name, rule).items()
    return dict(result)

if __name__ == "__main__":
    response = requests.get(url="http://xw.kunming.cn/a/2017-10/18/content_4805641.htm")
    extract_rules = {
        'title': {
            'method': 'xpath',
            'extract_rules': ('//*[@class="list_left"]/h1', ),
            'in': ('get_text', 'unicode'),
            'out': 'takefirst'
        },
        'body': {
            'method': 'xpath',
            'extract_rules': ('//*[@class="detailbg"]', ),
            'in': ('get_text', 'unicode'),
            'out': 'takefirst'
        },
        'pub_time': {
            'method': 'xpath',
            'extract_rules': ('//*[@id="pubtime_baidu"]', ),
            'in': ('get_text', 'unicode'),
            'out': 'takefirst'
        }
    }
    a1 = time.time()
    print parser(response.text, extract_rules)
    a2 = time.time()
    print a2 - a1

